package com.forum.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.forum.article.vo.CommentVO;
import com.forum.article.entity.ForumComment;

import com.forum.article.mapper.ForumCommentMapper;
import com.forum.article.service.ForumCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.article.vo.Result;
import com.forum.article.vo.param.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@Service
public class ForumCommentServiceImpl extends ServiceImpl<ForumCommentMapper, ForumComment> implements ForumCommentService {
    @Autowired
    ForumCommentMapper forumCommentMapper;



    //根据文章id查询评论列表
    @Override
    public Result commentByArticleId(Long id) {
        LambdaQueryWrapper<ForumComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ForumComment::getArticleId,id);
        queryWrapper.eq(ForumComment::getCommentLevel,1);
        queryWrapper.orderByDesc(ForumComment::getCreateTime);
        List<ForumComment> forumComments = forumCommentMapper.selectList(queryWrapper);
        List<CommentVO> commentVOList = copyList(forumComments);

        return Result.success(commentVOList);
    }

    @Override
    public Result comment(CommentParam commentParam) {

        ForumComment forumComment = new ForumComment();
        forumComment.setArticleId(commentParam.getArticleId());
        forumComment.setCommentContent(commentParam.getCommentContent());
        forumComment.setCreateTime(new Date());
        Long parent = commentParam.getParentId();
        if (parent == null || parent == 0) {
            forumComment.setCommentLevel(1);
        } else {
            forumComment.setCommentLevel(2);
        }
        forumComment.setParentId(parent == null ? 0 : parent);

        this.forumCommentMapper.insert(forumComment);

        return Result.success(null);
    }




    private List<CommentVO> copyList(List<ForumComment> forumComments) {
        List<CommentVO> commentVOList = new ArrayList<>();
        for (ForumComment forumComment : forumComments) {
            commentVOList.add(copy(forumComment));

        }
        return commentVOList;
    }

    private CommentVO copy(ForumComment forumComment) {
        CommentVO commentVO = new CommentVO();
        BeanUtils.copyProperties(forumComment,commentVO);
        commentVO.setId(Long.valueOf(forumComment.getId()));


//        子
        Integer commentLevel = forumComment.getCommentLevel();
        if (1 == commentLevel){
            Long id = forumComment.getId();
            List<CommentVO> commentVOList = findCommentByParentId(id);
            commentVO.setChildren(commentVOList);
        }

        if (commentLevel > 1){

        }
        return commentVO;


    }

    private List<CommentVO> findCommentByParentId(Long id){
        LambdaQueryWrapper<ForumComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ForumComment::getParentId,id);
        queryWrapper.eq(ForumComment::getCommentLevel,2);
        return copyList(forumCommentMapper.selectList(queryWrapper));
    }
}


